import router from './router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (store.getters.token) { // 有
    if (to.path === '/login') { // 判断是否去登录页
      next('/')
      NProgress.done()
    } else {
      next()
    }
  } else { // 没有
    // 判断是否在白名单
    if (whiteList.includes(to.path)) { // 在
      next()
    } else { // 不在
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
